const { exec } = require("child_process");
const curl =
  
// start();
function start() {
  // setTimeout(() => {
  exec(curl, (err, stdout, stderr) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log(`stderr: ${stderr}`);
    if (stdout) {
      const res = JSON.parse(stdout);
      const rooms = res.Data.Rows.filter((e) => {
        return (
          ((e.RoomTypeName === "1室0厅1卫" && e.MonthlyRent < 2100) ||
            (e.RoomTypeName === "1室1厅1卫" && e.MonthlyRent < 2300) ||
            (e.RoomTypeName === "2室1厅1卫" && e.MonthlyRent < 3200) ||
            (e.RoomTypeName === "2室0厅1卫" && e.MonthlyRent < 3200)) &&
          e.peocount < 3
        );
      });
      if (rooms.length === 0) {
        start();
      }
      send(rooms);
    }
  });
  // }, (Math.floor(Math.random() * 10) + 10) * 60 * 1000);
}

async function send(rooms) {
  console.log("🚀 ~ file: request.js:58 ~ send ~ rooms:", rooms);
  const nodemailer = require("nodemailer");

  let transporter = nodemailer.createTransport({
    host: "smtp.163.com",
    secure: true,
    secureConnection: true, // 使用了 SSL
    auth: {
      user: "awxy115@163.com",
      pass: "QDEODRASHRIELOPC",
    },
  });

  let mailOptions = {
    from: "awxy115@163.com", // sender address
    to: "awxy115@163.com", // list of receivers
    subject: "有新的room", // Subject line
    // text: 'Hello world?', // plain text body
    html: "<b>Hello world?</b>", // html body
  };
  const html = rooms.map((e) => {
    return `<h4>小区名称：${e.PropertyName}</h4>
    <h4>楼层${e.FloorName}</h4>
    <h4>房间类型${e.RoomTypeName}</h4>
    <h4>已抢人数${e.peocount}</h4>
    <h4>开抢时间${e.begdate} --- ${e.enddate}</h4>
    `;
  });
  mailOptions.html = html.join("\n------------------------------------");
  try {
    await transporter.sendMail(mailOptions);
  } catch (err) {
    console.log("🚀 ~ file: request.js:90 ~ send ~ err:", err);
  }
}

function getUrl(){

}
